package com.yanqu.road.logic.pb;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.unionChest.CrossUnionChestNoticeData;
import com.yanqu.road.entity.activity.unionChest.UnionChestBoxInfo;
import com.yanqu.road.entity.rank.ServerRank;
import com.yanqu.road.entity.rank.cross.CrossUnionActivityRank;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.pb.activity.ActivityProto;
import com.yanqu.road.pb.activity.UnionChestProto;
import com.yanqu.road.pb.cross.activity.CrossActivityProto;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CrossActivityPb {
    public static CrossActivityProto.CrossActivityServerRankTempMsg.Builder parseCrossActivityServerRankTempMsg(ServerRank serverRank){
        if(null == serverRank){
            return null;
        }
        CrossActivityProto.CrossActivityServerRankTempMsg.Builder msg = CrossActivityProto.CrossActivityServerRankTempMsg.newBuilder();
        msg.setServerId(serverRank.getServerId());
        msg.setValue(serverRank.getValue().toString());
        return msg;
    }

    public static CrossActivityProto.CrossActivityServerRankListMsg.Builder parseCrossActivityServerRankListMsg(List<ServerRank> serverRankList){
        CrossActivityProto.CrossActivityServerRankListMsg.Builder listMsg = CrossActivityProto.CrossActivityServerRankListMsg.newBuilder();
        if(null != serverRankList){
            for(ServerRank serverRank : serverRankList){
                CrossActivityProto.CrossActivityServerRankTempMsg.Builder msg = parseCrossActivityServerRankTempMsg(serverRank);
                if(null != msg){
                    listMsg.addRankList(msg);
                }
            }
        }
        return listMsg;
    }

    public static CrossActivityProto.CrossActivityUnionRankListMsg.Builder parseUnionRankListMsg(List<CrossUnionActivityRank> unionRankList){
        CrossActivityProto.CrossActivityUnionRankListMsg.Builder listMsg = CrossActivityProto.CrossActivityUnionRankListMsg.newBuilder();
        if(null != unionRankList){
            for(CrossUnionActivityRank unionRank : unionRankList){
                CrossActivityProto.CrossActivityUnionRankTempMsg.Builder msg = parseRankTempMsg(unionRank);
                if(null != msg){
                    listMsg.addRankList(msg);
                }
            }
        }
        return listMsg;

    }

    public static CrossActivityProto.CrossActivityUnionRankTempMsg.Builder parseRankTempMsg(CrossUnionActivityRank unionRank){
        if(null == unionRank){
            return null;
        }
        CrossActivityProto.CrossActivityUnionRankTempMsg.Builder msg = CrossActivityProto.CrossActivityUnionRankTempMsg.newBuilder();
        msg.setUnionUid(unionRank.getUnionUid());
        msg.setValue(unionRank.getValue().toString());
        msg.setUnionBaseData(UnionBasePb.parseUnionBaseTempMsg(unionRank.getUnionBaseInfo()));
        return msg;
    }

    public static CrossUnionActivityRank  parseCrossUnionRank(int activityId, CrossActivityProto.CrossActivityUnionRankTempMsg msg){
        CrossUnionActivityRank unionRank  = new CrossUnionActivityRank();
        unionRank.setValue(new BigInteger(msg.getValue()));
        unionRank.setUnionUid(msg.getUnionUid());
        UnionBaseInfo unionBaseInfo = new UnionBaseInfo();
        UnionProto.UnionBaseTempMsg unionBaseTempMsg = msg.getUnionBaseData();
        unionBaseInfo.setUnionName(unionBaseTempMsg.getUnionName());
        if(unionBaseTempMsg.getUnionFlagCount() > 1){
            unionBaseInfo.setUnionFlagStr(StringUtils.listToString(unionBaseTempMsg.getUnionFlagList(), StringUtils.SPLIT_SEPARATOR));
        }else {
            unionBaseInfo.setUnionFlag(unionBaseTempMsg.getUnionFlag(0));
        }
        //unionBaseInfo.setHonors(msg.get);
        //unionBaseInfo.setUnionFlag(unionBaseTempMsg.getUnionFlag());
        unionBaseInfo.setUnionAliasId(unionBaseTempMsg.getUnionAliasId());
        unionBaseInfo.setTotalEarnSpeed(BigInteger.valueOf(Long.valueOf(unionBaseTempMsg.getTotalEarnSpeed())));
        unionBaseInfo.setServerId(unionBaseTempMsg.getServerId());
        unionBaseInfo.setMemberCount(unionBaseTempMsg.getMemberCount());
        unionBaseInfo.setMasterVipExp(unionBaseTempMsg.getMasterVipExp());
        unionBaseInfo.setMasterUserId(unionBaseTempMsg.getMasterUserId());
        unionBaseInfo.setMasterNickName(unionBaseTempMsg.getMasterNickName());
        unionBaseInfo.setMasterDecoration(unionBaseTempMsg.getMasterDecoration());
        unionBaseInfo.setExperience(unionBaseTempMsg.getExperience());
        unionBaseInfo.setAllowRandomJoin(unionBaseTempMsg.getIsAllowRandomJoin());
        unionBaseInfo.setHideVipAndAddress(unionBaseTempMsg.getHideMasterVipAndAddress());

        unionRank.setActivityId(activityId);
        unionRank.setUnionBaseInfo(unionBaseInfo);
        unionRank.setServerId(msg.getUnionBaseData().getServerId());
        return unionRank;
    }

    public static List<UnionChestProto.UnionChestBoxRewardTemp> parseUnionChestBoxRewardListMsg(Map<Integer, UnionChestBoxInfo> infoMap){
        List<UnionChestProto.UnionChestBoxRewardTemp> msgList = new ArrayList<>();
        for (UnionChestBoxInfo boxInfo : infoMap.values()) {
            UnionChestProto.UnionChestBoxRewardTemp.Builder builder = UnionChestProto.UnionChestBoxRewardTemp.newBuilder();
            builder.setUserId(boxInfo.getOpenUserId());
            builder.setName(boxInfo.getOpenName());
            builder.setReward(boxInfo.getRewards());
            builder.setOpenTime(boxInfo.getOpenTime());
            builder.setBoxIndex(boxInfo.getId());
        }
        return msgList;
    }

    public static UnionChestProto.UnionChestBoxRewardTemp.Builder parseUnionChestBoxRewardMsg(UnionChestBoxInfo boxInfo){
        UnionChestProto.UnionChestBoxRewardTemp.Builder builder = UnionChestProto.UnionChestBoxRewardTemp.newBuilder();
        if (boxInfo.getOpenTime() > 0){
            builder.setUserId(boxInfo.getOpenUserId());
            builder.setName(boxInfo.getOpenName());
            builder.setReward(boxInfo.getRewards());
            builder.setOpenTime(boxInfo.getOpenTime());
        }
        builder.setBoxIndex(boxInfo.getId());
        return builder;
    }

    public static UnionChestProto.UnionChestNoticeTemp.Builder parseUnionChestNoticeMsg(CrossUnionChestNoticeData noticeData){
        UnionChestProto.UnionChestNoticeTemp.Builder noticeBuilder = UnionChestProto.UnionChestNoticeTemp.newBuilder();
        noticeBuilder.setBoxId(noticeData.getBoxId());
        noticeBuilder.setBoxType(noticeData.getBoxType());
        noticeBuilder.setShareName(noticeData.getShareName());
        noticeBuilder.setUserName(noticeData.getOpendName());
        noticeBuilder.setUserId(noticeData.getOpendUserId());
        noticeBuilder.setRewards(noticeData.getRewards());
        noticeBuilder.setOpenTime(noticeData.getOpendTime());
        return noticeBuilder;
    }

    public static ActivityProto.ActivityTempMsg.Builder parseActivityTempMsg(ActivityInfo activityInfo) {
        ActivityProto.ActivityTempMsg.Builder msg = ActivityProto.ActivityTempMsg.newBuilder();
        msg.setActivityId(activityInfo.getActivityId());
        msg.setResetType(Integer.parseInt(activityInfo.getParam2()));
        msg.setActivityType(activityInfo.getType());
        msg.setChildType(activityInfo.getChildType());
        msg.setBeginShowTime(activityInfo.getBeginShowTime());
        msg.setEndShowTime(activityInfo.getEndShowTime());
        msg.setBeginTime(activityInfo.getBeginTime());
        msg.setEndTime(activityInfo.getEndTime());
        msg.setActivityName(activityInfo.getActivityName());
        msg.setIcon(activityInfo.getIcon());
        msg.setPic(activityInfo.getParam1());
        msg.setActivityDesc(activityInfo.getActivityDesc());
        msg.setOrder(activityInfo.getOrder());
        msg.setExpandParam(activityInfo.getParam6());
        msg.addAllServerList(activityInfo.getServerIdList());
        msg.addAllServerList2(activityInfo.getServerIdList2());
        msg.setUnionGroup(activityInfo.getUnionGroup());
        msg.setConditionText(activityInfo.getParam3());
        msg.setUnit(activityInfo.getParam4());
        msg.setGoToScene(activityInfo.getParam5());
        return msg;
    }

    public static ActivityInfo parseActivityTempMsg(ActivityProto.ActivityTempMsg tempMsg) {
        ActivityInfo activityInfo = new ActivityInfo();

        activityInfo.setActivityId(tempMsg.getActivityId());
        activityInfo.setParam2(String.valueOf(tempMsg.getResetType()));
        activityInfo.setType(tempMsg.getActivityType());
        activityInfo.setChildType(tempMsg.getChildType());
        activityInfo.setBeginShowTime(tempMsg.getBeginShowTime());
        activityInfo.setEndShowTime(tempMsg.getEndShowTime());
        activityInfo.setBeginTime(tempMsg.getBeginTime());
        activityInfo.setEndTime(tempMsg.getEndTime());

        activityInfo.setIcon(tempMsg.getIcon());
        activityInfo.setParam1(tempMsg.getPic());

        activityInfo.setOrder(tempMsg.getOrder());
        activityInfo.setParam6(tempMsg.getExpandParam());
        activityInfo.setServerIdList(tempMsg.getServerListList());
        activityInfo.setServerIdList2(tempMsg.getServerList2List());
        activityInfo.setUnionGroup(tempMsg.getUnionGroup());
        activityInfo.setServerListStr2(StringUtils.listToString(tempMsg.getServerList2List(), ","));
        return activityInfo;
    }
}
